<?php

namespace ManaPHP\Http\Server\Adapter;

use ManaPHP\Helper\Ip;

/**
 * @property-read \ManaPHP\Http\RouterInterface $router
 */
class Php extends Fpm
{
    /**
     * @param array $options
     */
    public function __construct($options = [])
    {
        $public_dir = $this->alias->resolve('@public');
        $local_ip = $this->_host === '0.0.0.0' ? Ip::local() : $this->_host;

        if (PHP_SAPI === 'cli') {
            if (DIRECTORY_SEPARATOR === '\\') {
                shell_exec("explorer.exe http://127.0.0.1:$this->_port" . ($this->router->getPrefix() ?: '/'));
            }
            $_SERVER['REQUEST_SCHEME'] = 'http';
            $index = @get_included_files()[0];
            $cmd = "php -S $this->_host:$this->_port -t $public_dir  $index";
            $this->log('info', $cmd);
            $this->log('info', "http://$local_ip:$this->_port" . ($this->router->getPrefix() ?: '/'));
            shell_exec($cmd);
            exit(0);
        } else {
            $_SERVER['SERVER_ADDR'] = $local_ip;
            $_SERVER['SERVER_PORT'] = $this->_port;
            $_SERVER['REQUEST_SCHEME'] = 'http';
            $_GET['_url'] = $_REQUEST['_url'] = urldecode(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
            $this->_root_files = $this->_getRootFiles();
            $this->_mime_types = $this->_getMimeTypes();
        }

        parent::__construct($options);
    }

    /**
     * @param \ManaPHP\Http\Server\HandlerInterface $handler
     *
     * @return static
     */
    public function start($handler)
    {
        $this->_prepareGlobals();

        if ($file = $this->_isStaticFile()) {
            $file = "$this->_doc_root/$file";
            if ((DIRECTORY_SEPARATOR === '/' ? realpath($file) : str_replace('\\', '/', realpath($file))) === $file) {
                $ext = pathinfo($file, PATHINFO_EXTENSION);
                $mime_type = $this->_mime_types[$ext] ?? 'application/octet-stream';
                header('Content-Type: ' . $mime_type);
                readfile($file);
            } else {
                header('HTTP/1.1 404 Not Found');
            }
        } else {
            $handler->handle();
        }

        return $this;
    }
}